package com.nny.alarm.menu;

import com.nny.alarm.R;
import com.uxwine.widget.MenuWindow;

import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MainMenu extends MenuWindow implements View.OnKeyListener
{
	View mAnchor;
	boolean mbShown;
	
	public MainMenu(Context ctx, View anchor) 
	{
		super(ctx);
		mAnchor = anchor;
		initView();
	}
	
	private void initView()
	{
		mbShown = false;
		View v = getContentView();
		v.setFocusable(true);
		v.setOnKeyListener(this);
		v.setFocusableInTouchMode(true);
		getBackground().setAlpha(0);
	}
	
	public void show(View anchor)
	{
		mbShown = true;
		showAsDropDown(anchor, anchor.getWidth(), -1);
	}
	
	public void show()
	{
		if(mbShown)
			show(mAnchor);
		else
			show(mAnchor);
	}
	
	public void toggle()
	{
		if(isShowing())
			dismiss();
		else
			show();
	}


	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) 
	{
		if(isShowing())
		{
			dismiss();
		}
		return false;
	}
	
	public void defaultMenu(Activity act)
	{
		setAdapter(new MAdapter(act));
		setOnWindowEventListener(new MainMenuAction(act));
	}
	
	class MAdapter extends BaseAdapter
	{
		Item[] mItems;
		Context mCtx;
		public MAdapter(Context ctx)
		{
			mCtx = ctx;
			mItems = new Item[]{
				//new Item(R.string.menu_setting_title, R.drawable.menu_settings),
				new Item(R.string.menu_share, R.drawable.menu_share),
				new Item(R.string.menu_help, R.drawable.menu_help),
				new Item(R.string.menu_about, R.drawable.menu_about)
			};
		}
		
		@Override
		public int getCount()
		{
			return mItems.length;
		}

		@Override
		public Object getItem(int position)
		{
			return mItems[position];
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent)
		{
			final Item i = (Item)getItem(position);
			if(null == v)
				v = View.inflate(mCtx, R.layout.li_icon_menu, null);
			TextView label = (TextView)v.findViewById(R.id.icon_menu_tv);
			ImageView icon = (ImageView)v.findViewById(R.id.icon_menu_iv);
			label.setText(i.label);
			icon.setImageResource(i.icon);
			return v;
		}
		

		class Item
		{
			Item(int label, int icon)
			{
				this.icon = icon;
				this.label = label;
			}
			int icon;
			int label;
		}
		
	}
}
